package com.yanqu.road.server.manger.activity.commonwish;

import com.yanqu.road.dao.impl.activity.commonwish.CommonWishConfigDaoImpl;
import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.commonwish.CommonWishConfig;
import com.yanqu.road.entity.enums.eActivityConfigType;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.commonwish.CommonWishModule;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CommonWishMgr extends TempMgr {


    private static Map<Integer, CommonWishConfig> configMap = new ConcurrentHashMap<>();

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    public static void reloadActivityData() {
        getLogger().info("reload common wish draw Activity start");

        List<ActivityInfo> openActivityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.CommonWishDraw.getValue());
        if (openActivityInfoList.isEmpty()) {
            getLogger().info("no common wish draw Activity in show time");
            configMap = new ConcurrentHashMap<>();
        } else {

            Map<Integer, CommonWishConfig> tempMap = new ConcurrentHashMap<>();

            for (ActivityInfo activityInfo : openActivityInfoList) {

                int tmpActivityId = activityInfo.getActivityId();

                CommonWishConfig configTemp = new CommonWishConfig();
                configTemp.setActivityInfo(activityInfo);

                //初始化配置
                Map<String, ActivityConfig> activityConfigMap = NormalActivityMgr.getActivityConfigMap(tmpActivityId);

                configTemp.setDrawInfoList(new CommonWishConfigDaoImpl().getFanXiuSpringDrawInfoList(tmpActivityId));

                if (activityConfigMap.containsKey(eActivityConfigType.WISHDRAW_SCORE_ITEM_ID.getName())) {
                    configTemp.setScoreItemId(activityConfigMap.get(eActivityConfigType.WISHDRAW_SCORE_ITEM_ID.getName()).getIntValue());
                } else if(activityConfigMap.containsKey(eActivityConfigType.EXCHANGE_SCORE_ITEM_ID.getName())){
                    configTemp.setScoreItemId(activityConfigMap.get(eActivityConfigType.EXCHANGE_SCORE_ITEM_ID.getName()).getIntValue());
                }
                if (activityConfigMap.containsKey(eActivityConfigType.WISHDRAW_PIC_ITEM_ID.getName())) {
                    configTemp.setPiceItemId(activityConfigMap.get(eActivityConfigType.WISHDRAW_PIC_ITEM_ID.getName()).getIntValue());
                }
                if (activityConfigMap.containsKey(eActivityConfigType.WISHDRAW_OPP_ADD.getName())) {
                    configTemp.setDrawOppAdd(activityConfigMap.get(eActivityConfigType.WISHDRAW_OPP_ADD.getName()).getIntValue());
                }
                if (activityConfigMap.containsKey(eActivityConfigType.WISHDRAW_OPP_MAX.getName())) {
                    configTemp.setDrawOppMax(activityConfigMap.get(eActivityConfigType.WISHDRAW_OPP_MAX.getName()).getIntValue());
                }
                if (activityConfigMap.containsKey(eActivityConfigType.WISHDRAW_RARE_POOL.getName())) {
                    configTemp.setDrawRarePool(activityConfigMap.get(eActivityConfigType.WISHDRAW_RARE_POOL.getName()).getValue());
                }
                if (activityConfigMap.containsKey(eActivityConfigType.WISHDRAW_SCORE_NUM.getName())) {
                    configTemp.setDrawScoreGet(activityConfigMap.get(eActivityConfigType.WISHDRAW_SCORE_NUM.getName()).getIntValue());
                }
                if (activityConfigMap.containsKey(eActivityConfigType.WISHDRAW_TEN.getName())) {
                    configTemp.setTenDrawScoreLimit(activityConfigMap.get(eActivityConfigType.WISHDRAW_TEN.getName()).getIntValue());
                }
                if (activityConfigMap.containsKey(eActivityConfigType.WISHDRAW_NOTICE.getName())) {
                    configTemp.setDrawNoticeCount(activityConfigMap.get(eActivityConfigType.WISHDRAW_NOTICE.getName()).getIntValue());
                }
                if (activityConfigMap.containsKey(eActivityConfigType.WISHDRAW_RARE_WEIGHT_PARAM.getName())) {
                    configTemp.setDrawRareWeight(activityConfigMap.get(eActivityConfigType.WISHDRAW_RARE_WEIGHT_PARAM.getName()).getValue());
                }
                if (activityConfigMap.containsKey(eActivityConfigType.WISHDRAW_ITEM_RECYCLE.getName())){
                    configTemp.setItemRecycle(activityConfigMap.get(eActivityConfigType.WISHDRAW_ITEM_RECYCLE.getName()).getValue());
                }

                if (activityConfigMap.containsKey(eActivityConfigType.WISHDRAW_DRAW_ITEM_ID.getName())){
                    configTemp.setDrawItemId(activityConfigMap.get(eActivityConfigType.WISHDRAW_DRAW_ITEM_ID.getName()).getIntValue());
                }

                if (activityConfigMap.containsKey(eActivityConfigType.QUICK_USE_NUM.getName())){
                    configTemp.setQuickUseNum(activityConfigMap.get(eActivityConfigType.QUICK_USE_NUM.getName()).getIntValue());
                }

                configTemp.afterInit();

                tempMap.put(tmpActivityId, configTemp);
            }

            configMap = tempMap;
        }

        getLogger().info("reload common wish draw Activity finish");

        // 在线玩家下发数据
        for (GamePlayer gamePlayer : GamePlayerMgr.getAllOnlinePlayer()) {
            CommonWishModule module = gamePlayer.getModule(CommonWishModule.class);
            module.syncConfig();
            module.initUserData();
            module.syncUserData();
        }
    }

    public static CommonWishConfig getCommonWishConfig(int activityId){
        return configMap.get(activityId);
    }

    public static Map<Integer, CommonWishConfig> getConfigMap() {
        return configMap;
    }
}
